package com.littlebee.storysuper.utils;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;

import com.zss.library.widget.CommonProgressDialog;

import org.json.JSONObject;

import java.io.File;
import java.util.Map;

/**网络交互工具类*/
public class NetWorkUtils {
	private static CommonProgressDialog dialog;

	/** 返回状态码*/
	private static String RET_CODE = "rc";
	/**返回失败码*/
	private static String RET_MSG = "rd";
	/** 返回状态成功码*/

	private static int SUC_FLAG = 0;

//	public static void get(final Activity mContext, String actionDo,Map<String, String> params,
//                           final IListener listener) {
//		get(mContext, actionDo, params,true, listener);
//	}

//	public static void post(final Activity mContext, String actionDo, Map<String, String> params,
//                            final IListener listener) {
//		post(mContext, actionDo, params, true, listener);
//	}

	/**
	 * get请求公有方法
	 * @param mContext 请求的页面
	 * @param actionDo 请求地址
	 * @param params 请求参数
	 * @param isLock 是否显示对话框 true：显示网络交互对话框  显示网络交互失败Toast提示
	 * @param listener 回调
	 */
	public static void get(final Activity mContext, String actionDo, Map<String, String> params, final boolean isLock, final IListener listener) {
		if(!NetWorkManager.isNetworkAvailable(mContext)){
			CommonToastUtils.showInCenterToast(mContext, "您当前网络不可用");
			listener.onError("您当前网络不可用", "", "");
			return;
		}
		if(isLock)
			showProgressDialog(mContext, "");
		OkHttpUtils.get(actionDo,params, new OkHttpUtils.ICallback(){
			@Override
			public void onSuccess(final String result) {
				mContext.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						try {
							//{"result":{"rc":-2,"rd":"密码错误"}}
							JSONObject resObj = new JSONObject(result);
							JSONObject object=resObj.getJSONObject("result");
							int code = object.optInt(RET_CODE);
							if(SUC_FLAG==code){
								listener.onSuccess(result, resObj);
							}else{
								String msg = object.optString(RET_MSG);
								listener.onError(result, ""+code, msg);
								if(isLock) {
									Utils.commonError(mContext, result, "" + code, msg);
								}
							}
						}catch (Exception e){
							e.printStackTrace();
						}finally {
							if(isLock)
								dismissProgressDialog(mContext);
						}
					}
				});

			}
			@Override
			public void onError(final String result) {
				mContext.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						listener.onError(result, "", "");
						if(isLock) {
							Utils.commonError(mContext, result, "", "");
							dismissProgressDialog(mContext);
						}
					}
				});
			}
		});
	}


	/**
	 * post请求公有方法
	 * @param mContext 请求的页面
	 * @param actionDo 请求地址
	 * @param params 请求参数
	 * @param isLock 是否显示对话框 true：显示网络交互对话框  显示网络交互失败Toast提示
	 * @param listener 回调
	 */
	public static void post(final Activity mContext, String actionDo, Map<String, String> params, final boolean isLock,
                            final IListener listener) {
		if(!NetWorkManager.isNetworkAvailable(mContext)){
			CommonToastUtils.showInCenterToast(mContext, "您当前网络不可用");
			listener.onError("您当前网络不可用", "", "");
			return;
		}
		if(isLock)
			showProgressDialog(mContext, "");
		OkHttpUtils.post(actionDo, params, new OkHttpUtils.ICallback(){
			@Override
			public void onSuccess(final String result) {
				mContext.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						try {
							JSONObject resObj = new JSONObject(result);
							JSONObject object=resObj.getJSONObject("result");
							int code = object.optInt(RET_CODE);
							if(SUC_FLAG==code){
								listener.onSuccess(result, resObj);
							}else{
								String msg = object.optString(RET_MSG);
								listener.onError(result, ""+code, msg);
								if(isLock) {
									Utils.commonError(mContext, result, "" + code, msg);
								}
							}
						}catch (Exception e){
							e.printStackTrace();
						}finally {
							if(isLock)
								dismissProgressDialog(mContext);
						}
					}
				});

			}
			@Override
			public void onError(final String result) {
				mContext.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						listener.onError(result, "", "");
						if(isLock){
							dismissProgressDialog(mContext);
							Utils.commonError(mContext, result, "", "");
						}
					}
				});
			}
		});
	}

	/**
	 * 上传文件公有方法
	 * @param mContext 调用页面
	 * @param actionDo 地址
	 * @param fileKey 文件key
	 * @param file 文件地址
	 * @param map 上床参数
	 * @param listener 回调
	 */
	public static void upload(final Activity mContext, String actionDo, String fileKey, File file,
                              Map<String, String> map, final IProgressListener listener) {
		if(!NetWorkManager.isNetworkAvailable(mContext)){
			CommonToastUtils.showInCenterToast(mContext, "您当前网络不可用");
			listener.onError("您当前网络不可用", "", "");
			return;
		}
		OkHttpUtils.upload(Constant.HOST + actionDo, fileKey, file, map, new OkHttpUtils.IUploadCallback() {
			@Override
			public void onSuccess(final String result) {
				mContext.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						try {
							JSONObject resObj = new JSONObject(result);
							int code = resObj.optInt(RET_CODE);
							if(SUC_FLAG==code){
								listener.onSuccess(result, resObj);
							}else{
								String msg = resObj.optString(RET_MSG);
								listener.onError(result, ""+code, msg);
								Utils.commonError(mContext, result, ""+code, msg);
							}
						}catch (Exception e){
							e.printStackTrace();
						}
					}
				});
			}

			@Override
			public void onProgress(final int index, final long totalBytes, final long residueBytes, final boolean done) {
				mContext.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						listener.onProgress(index, totalBytes, residueBytes, done);
					}
				});
			}

			@Override
			public void onError(final String result) {
				mContext.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						listener.onError(result, "", "");
						Utils.commonError(mContext, result, "", "");
						dismissProgressDialog(mContext);
					}
				});
			}
		});
	}

	public interface IProgressListener {

		public void onSuccess(String result, JSONObject resObj);

		public void onProgress(int index, long totalBytes, long surplusBytes, boolean done);

		public void onError(String result, String code, String msg);
	}

	public interface IListener {

		public void onSuccess(String result, JSONObject resObj);

		public void onError(String result, String code, String msg);
	}

	public static void showProgressDialog(Context mContext, final String showStr) {
		if (mContext != null) {
			if (dialog == null) {
				dialog = new CommonProgressDialog(mContext);
				dialog.setTitle(showStr);
				dialog.setCancelable(true);
				dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
					@Override
					public boolean onKey(DialogInterface d, int keyCode, KeyEvent event) {
						if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
							dialog.dismiss();
							dialog = null;
						}
						return false;
					}
				});
				dialog.show();
			} else {
				if (!dialog.isShowing()) {
					dialog.show();
				}
			}
		}
	}

	public static void dismissProgressDialog(Context mContext) {
		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}
	}

}
